package org.xinjiang.data.migration.xinjiang.convert;

import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.xinjiang.data.migration.common.constant.CreateConstant;
import org.xinjiang.data.migration.common.enums.YesNoEnum;
import org.xinjiang.data.migration.common.mapstruct.CommonConvertConfig;
import org.xinjiang.data.migration.xinjiang.dto.res.XjJzdxResultDTO;
import org.xinjiang.data.migration.xinjiang.dto.res.XjSubJzdxResultDTO;
import org.xinjiang.data.migration.xinjiang.entity.XjJzdxEntity;

import java.util.Objects;

@Mapper(config = CommonConvertConfig.class)
public abstract class XjJzdxConvert {
    public abstract XjJzdxResultDTO convert(XjJzdxEntity s);

    public void postAddAttribute(XjSubJzdxResultDTO record) {
        if (Objects.isNull(record)) {
            return;
        }

        //港澳台身份证类型
        identityType(record);

        //文化程度
        record.setWhcd(eduLevelChange(record.getWhcd()));

        //设置五独、五涉、四史
        referTo(record);

        //设置是否有前科,为了简单，统一设置为无
        record.setSfyqk(YesNoEnum.NO.getCode());

        //设置犯罪类型
        setCrimeTypes(record);

        //设置手机号
        setPersonalMobile(record);

        //设置原判刑罚为汉字的的时候
        record.setYpxf(changePenalty(record.getYpxf()));

        //捕前职业转换
        record.setPqzy(occupationChange(record.getPqzy()));

        //设置创建人
        record.setCreateBy(CreateConstant.SYSTEM);

        //设置状态
        record.setZt(statusChange(record.getZt()));

        //设置国籍
        setNation(record);
    }

    /**
     * 原判刑罚默认为有期
     *
     * @param source 原判刑罚
     */
    public String changePenalty(String source) {
        if (StringUtils.isEmpty(source) || isPositiveInteger(source)) {
            return "3";
        }

        return source;
    }

    /**
     * 判断是否有汉字（正整数），多犯罪类型的先忽略
     *
     * @param str 原始的犯罪类型
     * @return 如果有汉字，返回true
     */
    private boolean isPositiveInteger(String str) {
        // 匹配一个或多个数字
        return str.matches(".*\\D+.*");
    }

    private void setPersonalMobile(XjSubJzdxResultDTO record) {
        if (StringUtils.isEmpty(record.getGrlxdh())) {
            record.setGrlxdh("无");
        }
    }

    private void setCrimeTypes(XjSubJzdxResultDTO record) {
        if (StringUtils.isNotEmpty(record.getFzlx())) {
            if (isPositiveInteger(record.getFzlx())) {
                record.setFzlx("");
                return;
            }

            if (record.getFzlx().contains(",")) {
                String[] type = record.getFzlx().split(",");
                for (int i = 0; i < type.length; i++) {
                    type[i] = crimeType(type[i]);
                }
                record.setFzlx(String.join(",", type));
            } else {
                String result = crimeType(record.getFzlx());
                record.setFzlx(result);
            }
        }
    }

    /**
     * id和code转换
     *
     * @param type 犯罪类型
     * @return String
     */
    private String crimeType(String type) {
        if (StringUtils.isEmpty(type)) {
            return "";
        }

        if (type.length() < 2) {
            return YesNoEnum.NO.getCode() + type;
        }

        return type;
    }

    private void identityType(XjSubJzdxResultDTO record) {
        if (Objects.equals(YesNoEnum.YES.getCode(), record.getYwgatsfz())) {
            if (Objects.isNull(record.getGatsfzlx()) || Objects.equals(CreateConstant.OTHER, record.getGatsfzlx())) {
                record.setGatsfzlx(CreateConstant.EMPTY_STRING);
            } else {
                record.setGatsfzlx(record.getGatsfzlx().substring(0, 1));
            }
        }
    }

    /**
     * 这里为了简单期间，统一设置为默认的
     *
     * @param record XjSubJzdxResultDTO
     */
    private void referTo(XjSubJzdxResultDTO record) {
        record.setSfwd("");
        record.setSfws("");
        record.setSfyss("");
    }

    private String eduLevelChange(String source) {
        String result = "99";
        if (StringUtils.isEmpty(source)) {
            return result;
        }
        switch (source) {
            case "1":
                result = "1";
                break;
            case "2":
                result = "2";
                break;
            case "3":
                result = "3";
                break;
            case "4":
                result = "4";
                break;
            case "5":
                result = "9";
                break;
            case "6":
                result = "5";
                break;
            case "7":
                result = "6";
                break;
            case "8":
                result = "7";
                break;
            case "9":
                result = "8";
                break;
        }

        return result;
    }

    /**
     * 捕前职业转换，将code转为id
     *
     * @param source 捕前职业
     * @return String
     */
    private String occupationChange(String source) {
        String result = "10";
        if (StringUtils.isEmpty(source)) {
            return result;
        }
        switch (source) {
            case "01":
                result = "1";
                break;
            case "02":
                result = "2";
                break;
            case "03":
                result = "3";
                break;
            case "04":
                result = "4";
                break;
            case "05":
                result = "5";
                break;
            case "06":
                result = "6";
                break;
            case "07":
                result = "7";
                break;
            case "08":
                result = "8";
                break;
            case "09":
                result = "9";
                break;
            case "0101":
                result = "11";
                break;
            case "0102":
                result = "12";
                break;
            case "0103":
                result = "13";
                break;
            case "0104":
                result = "14";
                break;
            case "0105":
                result = "15";
                break;
            case "0301":
                result = "17";
                break;
            case "0302":
                result = "18";
                break;
            case "0401":
                result = "19";
                break;
        }

        return result;
    }

    /**
     * 272,正常
     * 608,其它
     * 528,矫正期满
     * 610,余漏罪
     * 560,违反矫正规定
     * 588,暂予监外执行收监
     * 544,死亡
     *
     * @param source 状态
     * @return String
     */
    private String statusChange(String source) {
        String result = "4";
        if (StringUtils.isEmpty(source)) {
            return result;
        }

        if (source.equals("272")) {
            result = "3";
        }

        return result;
    }

    /**
     * when '1' then
     * '11'
     * when '2' then
     * '12'
     * when '3' then
     * (case substr(t.gangaotai_leixing, 1, 1)
     * when '1' then
     * '16'
     * when '2' then
     * '14'
     * when '3' then
     * '15'
     * else
     * '16'
     * end)
     * when '4' then
     * '2'
     * when '5' then
     * '3'
     * else
     * '13
     *
     * @param record
     */
    private void setNation(XjSubJzdxResultDTO record) {

        String nation = "1";
        if (StringUtils.isNotEmpty(record.getGj())) {

            switch (record.getGj()) {
                case "1":
                    nation = "4";
                    break;
                case "2":
                    nation = "5";
                    break;
                case "4":
                    nation = "2";
                    break;
                case "5":
                    nation = "3";
                    break;
            }
        }

        record.setGj(nation);

        //设置港澳台
        if (Objects.equals("3", record.getGj()) && StringUtils.isNotEmpty(record.getGatsfzlx())) {
            String lx = record.getGatsfzlx().substring(0, 1);
            String nat = "7";
            switch (lx) {
                case "1":
                    nat = "7";
                    break;
                case "2":
                    nat = "8";
                    break;
                case "3":
                    nat = "9";
                    break;
            }

            record.setGj(nat);
        }

    }
}
